#pragma once

#include "PngWrapper.h"
#include "Plotter.h"
#include "Color.h"
#include <string>
#include <cfloat>
namespace HW2
{

struct BackgroundImage
{
	PngWrapper wrapper;
	bool repeat; // true for repeat, false for stretch
	bool valid;

	BackgroundImage() : valid(false) {}

	BackgroundImage(std::string &filename, bool repeat) : wrapper(filename.c_str()), repeat(repeat), valid(true)
	{
		wrapper.ReadPng();
	}

	Color int2Color(int a)
	{
		return Color(GET_R(a), GET_G(a), GET_B(a));
	}

	void draw(Plotter &p, int width, int height, int scaling)
	{
		if(!valid)
			return;
		for(int x = 0; x < width; ++x)
		{
			for(int y = 0; y < height; ++y)
			{
				Color c;
				if(repeat)
				{
					c = int2Color(wrapper.GetValue(x%wrapper.GetWidth(), y % wrapper.GetHeight()));
				}
				else
				{
					double xRatio = (double)wrapper.GetWidth() / width;
					double yRatio = (double)wrapper.GetHeight() / height;
					c = int2Color(wrapper.GetValue(min(x * xRatio,wrapper.GetWidth()-1), min(y * yRatio, wrapper.GetHeight()-1)));
				}
				for(int i = 0; i < scaling; ++i)
				{
					for(int j = 0; j < scaling; ++j)
						p.plot(x*scaling+i,y*scaling+j,DBL_MAX ,c);
				}
			}

		}
	}
	
};

}